package com.hanlin.work;

import com.hanlin.RabbitUtils;
import com.rabbitmq.client.*;

import java.io.IOException;

/**
 * 描述：消费者
 *
 * @author:hl.yuan
 * @date:2021-03-13
 */
public class ConsumerRabbitMQ2 {


    public static void main(String[] args) throws IOException {
        Connection connection = RabbitUtils.getConnection();
        Channel channel = connection.createChannel();
         // 一次接受一条消息
        channel.basicQos(1);
        channel.queueDeclare("hello",true,false,false,null);
        // 参数2：false 关闭自动提交事务
        channel.basicConsume("hello",false,new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("消费者2: "+new String(body));
                try {
                    Thread.sleep(1000L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                // 手动确认消息。参数一：手动消息标识；参数二：每次确认一个
                channel.basicAck(envelope.getDeliveryTag(),false);
            }
        });

    }



}
